//
//  NSString+CKString.h
//  ConnectionKit
//
//  Created by Matthias Plappert on 13.06.09.
//  Copyright 2009 phapswebsolutions. All rights reserved.
//

#import "NSString+CKString.h"

@implementation NSString (CKString)

- (NSString *)md5Hash
{
	// Source: http://miketeo.net/wp/index.php/2008/09/22/calculating-md5-digest-with-iphone-sdk.html
	
	const char *cStr = [self UTF8String];
	unsigned char result[CC_MD5_DIGEST_LENGTH];
	CC_MD5(cStr, strlen(cStr), result);
	
	return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4],
																											result[5], result[6], result[7],
																											result[8], result[9], result[10], result[11], result[12],
																											result[13], result[14], result[15]] lowercaseString];
}

- (NSString *)stripHTMLTags
{
	// Source: http://www.rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c
  
	NSScanner *theScanner;
    NSString *text = nil;
	NSString *result = self;
	
    theScanner = [NSScanner scannerWithString:self];
	
    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL]; 
		
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text];
		
        // replace the found tag
        result = [result stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text]
												   withString:@""];
    }
    
    return result;
	
}

@end
